package com.wgu.crawl2.web;

import com.wgu.common.global.Result;
import com.wgu.crawl2.service.CrawlService2;
import com.wgu.crawl2.service.XBQGBookService;
import com.wgu.xbqg.XBQGBook;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 新笔趣阁：www.xbiquge.la
 * 爬虫控制器
 * Created by w on 2019/6/27.
 */
@ApiIgnore
@RestController
@RequestMapping("/api/crawl2")
public class CrawlController2 {
    @Resource
    private CrawlService2 crawlService2;
    @Resource
    private XBQGBookService xbqgBookService;
    /**
     * 爬取书籍
     * @return
     */
    @GetMapping("/book")
    public Result book(){
        crawlService2.crawlBook();
        return Result.ok();
    }

    /**
     * 爬取章节列表
     * @return
     */
    @GetMapping("/chapterList")
    public Result chapterList(Pageable pageable){
        List<XBQGBook> list = xbqgBookService.list(pageable);
        list.forEach((xbqgBook) -> crawlService2.crawlChapterList(xbqgBook));
        return Result.ok();
    }

    /**
     * 爬取章节内容
     * @param pageable
     * @return
     */
    @GetMapping("/chapterDetail")
    public Result chapterDetail(Pageable pageable){
        List<XBQGBook> list = xbqgBookService.list(pageable);
        list.forEach((xbqgBook) -> crawlService2.crawlChapterDetail(xbqgBook));
        return Result.ok();
    }

    @GetMapping("/demo")
    public Result demo(){
        return Result.ok(crawlService2.demo());
    }
}
